import MySql.Goods;
import MySql.Introduces;
import MySql.goodsManager;
import MySql.introducesManager;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class Set_IntroduceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        createCookies("1","1",request,response);
        createCookies("2","10",request,response);
        createCookies("3","11",request,response);
        response.sendRedirect("PetHouse.jsp");
    }

    public void createCookies(String no,String id,HttpServletRequest request, HttpServletResponse response){
        try {
            Introduces introduces= introducesManager.getRowById(id);
            PrintWriter out=response.getWriter();
            out.println(introduces);
            Cookie cookie1=new Cookie("introduce-"+no+"-id",introduces.getId());
            Cookie cookie2=new Cookie("introduce-"+no+"-kind",introduces.getKind());
            Cookie cookie3=new Cookie("introduce-"+no+"-title",introduces.getTitle());
//            out.println(introduces.getContent());
            out.print("</br>");
            out.print("</br>");
            out.print("</br>");
            Cookie cookie4=new Cookie("introduce-"+no+"-content",introduces.getContent());
            out.println(cookie4.getValue());
            //            Cookie cookie5=new Cookie("introduce-"+no+"-img",introduces.getImg());

            cookie1.setMaxAge(60*60*60*60);
            cookie2.setMaxAge(60*60*60*60);
            cookie3.setMaxAge(60*60*60*60);
            cookie4.setMaxAge(60*60*60*60);
//            cookie5.setMaxAge(60*60*60*60);

            cookie1.setPath(request.getContextPath() + "/");
            cookie2.setPath(request.getContextPath() + "/");
            cookie3.setPath(request.getContextPath() + "/");
            cookie4.setPath(request.getContextPath() + "/");
//            cookie5.setPath(request.getContextPath() + "/");
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            response.addCookie(cookie3);
            response.addCookie(cookie4);


//            Cookie[] cookies=request.getCookies();
//            for(Cookie cookie:cookies){
//                if (cookie.getName().equals("introduce-1-content")){
//                    out.println("ohhhhhhhhhhhhhhhh");
//                    out.println(cookie.getValue());
//                }
//                if (cookie.getName().equals("introduce-2-content")){
//                    out.println("yes!I'm here");
//                    out.println(cookie.getValue());
//                }
//            }


//            response.addCookie(cookie5);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
